<template>
    <el-dialog
            :title="!dataForm.driverId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="司机姓名" prop="driverName">
                        <el-input v-model="dataForm.driverName" placeholder="司机姓名"></el-input>
                </el-form-item>
                <el-form-item label="司机联系电话" prop="driverPhone">
                        <el-input v-model="dataForm.driverPhone" placeholder="司机联系电话"></el-input>
                </el-form-item>
                <el-form-item label="司机驾驶证号码" prop="driverLicense">
                        <el-input v-model="dataForm.driverLicense" placeholder="司机驾驶证号码"></el-input>
                </el-form-item>
                <el-form-item label="司机状态" prop="driverStatus">
                    <el-select v-model="dataForm.driverStatus" placeholder="请选择车辆状态">
                        <el-option label="空闲" value="空闲"></el-option>
                        <el-option label="运输中" value="运输中"></el-option>
                    </el-select>
                </el-form-item>                
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            driverId: null,
            driverName: null,
            driverPhone: null,
            driverLicense: null,
            carrierId: null,
            driverStatus: null
          },
          dataRule: {
            driverId: [
                        { required: true, message: '司机ID，主键不能为空', trigger: 'blur' }
            ],
            driverName: [
                        { required: true, message: '司机姓名不能为空', trigger: 'blur' }
            ],
            driverPhone: [
                        { required: true, message: '司机联系电话不能为空', trigger: 'blur' }
            ],
            driverLicense: [
                        { required: true, message: '司机驾驶证号码不能为空', trigger: 'blur' }
            ],
            carrierId: [
                        { required: true, message: '所属承运商ID，外键关联承运商表不能为空', trigger: 'blur' }
            ],
            driverStatus: [
                        { required: true, message: '司机状态不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.driverId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.driverId = id || 0
            if (this.dataForm.driverId) {
              this.$http({
                url: this.$http.adornUrl(`/app/drivers/info/${this.dataForm.driverId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appdrivers
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/drivers/${!this.dataForm.driverId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>